<template>
  <div class="media-reports">
    <div
      class="page-header"
      :style="bannerImage ? { backgroundImage: `url(${bannerImage})` } : {}"
      :class="{ 'has-bg': !!bannerImage }"
    >
      <div class="container">
        <h1 class="page-title">媒体报道</h1>
        <p class="page-subtitle">媒体对展会的专业报道和深度分析</p>
      </div>
    </div>
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <div class="container">
        <nav class="about-nav">
          <a
            @click="setActiveTab('exhibition-news')"
            :class="['nav-item', { active: activeTab === 'exhibition-news' }]"
          >
            展会资讯
          </a>
          <a
            @click="setActiveTab('media-reports')"
            :class="['nav-item', { active: activeTab === 'media-reports' }]"
          >
            媒体报道
          </a>
          <a
            @click="setActiveTab('industry-news')"
            :class="['nav-item', { active: activeTab === 'industry-news' }]"
          >
            行业资讯
          </a>
          <a
            @click="setActiveTab('on-site-highlights')"
            :class="[
              'nav-item',
              { active: activeTab === 'on-site-highlights' },
            ]"
          >
            现场集锦
          </a>
          <a
            @click="setActiveTab('cooperative-media')"
            :class="['nav-item', { active: activeTab === 'cooperative-media' }]"
          >
            合作媒体
          </a>
          <a
            @click="setActiveTab('media-registration')"
            :class="[
              'nav-item',
              { active: activeTab === 'media-registration' },
            ]"
          >
            媒体注册
          </a>
        </nav>
      </div>
    </div>

    <div class="container">
      <section class="content-section">
        <!-- <h2 class="section-title">媒体报道</h2>
        <p v-if="pageData.description" class="section-desc">
          {{ pageData.description }}
        </p> -->

        <!-- 多条数据展示：图片在上，文本在下 -->
        <div class="reports-grid">
          <div
            class="report-item"
            v-for="report in mediaReports"
            :key="report.id"
            @click="goDetail(report)"
          >
            <!-- 文本在下 -->
            <div class="report-content">
              <div class="report-t-b">
                <el-icon color="#007bff"><DArrowRight /></el-icon>
                <h3 class="report-title">{{ report.title }}</h3>
              </div>
              <p class="report-source">{{ report.source }}</p>
            </div>
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { subpageApi } from "@/api/user";
import { useRouter } from "vue-router";
import { DArrowRight } from "@element-plus/icons-vue";
import { encrypt } from "@/utils/cryptoJS";

export default {
  name: "MediaReports",
  components: {
    DArrowRight,
  },
  setup() {
    const router = useRouter();
    const activeTab = ref("media-reports");
    // 页面基本信息
    const pageData = ref({});
    const bannerImage = ref("");

    // 媒体报道数据
    const mediaReports = ref([]);

    // 解析富文本内容
    const parseContent = (content) => {
      if (!content)
        return { content: "", excerpt: "", image: "", source: "", link: "" };

      try {
        if (typeof content === "string" && content.trim().startsWith("{")) {
          return JSON.parse(content);
        }
        return {
          content: content,
          excerpt: "",
          image: "",
          source: "",
          link: "",
        };
      } catch (e) {
        return {
          content: content,
          excerpt: "",
          image: "",
          source: "",
          link: "",
        };
      }
    };

    // 加载媒体报道数据
    const loadMediaReports = async () => {
      try {
        const response = await subpageApi.getSubpageList(
          "media",
          "media-reports"
        );
        if (
          response &&
          response.code === "200" &&
          response.data &&
          response.data.length > 0
        ) {
          // 取第一条作为页面基本信息
          const firstItem = response.data[0];
          pageData.value = {
            title: firstItem.title || "媒体报道",
            description:
              firstItem.description ||
              (firstItem.content &&
              !(firstItem.content || "").trim().startsWith("[") &&
              !(firstItem.content || "").trim().startsWith("{")
                ? firstItem.content
                : ""),
          };

          // 处理多条媒体报道数据
          mediaReports.value = response.data.map((item, index) => {
            console.log(item);

            const parsed = parseContent(item.content);
            return {
              id: item.id || index + 1,
              title: item.title || "",
              source: parsed.activeTime || "",
              content: parsed.content || item.description || "",
              image: item.imageUrl || parsed.image || "",
              link: item.linkUrl || "",
            };
          });
        } else {
          // 使用默认数据
          pageData.value = getDefaultPageData();
          mediaReports.value = getDefaultMediaReports();
        }
      } catch (error) {
        console.error("加载媒体报道数据失败:", error);
        pageData.value = getDefaultPageData();
        mediaReports.value = getDefaultMediaReports();
      }
    };

    const loadBanner = async () => {
      try {
        const res = await subpageApi.getContentByType("carousel_media");
        if (
          res &&
          (res.code === "200" || res.code === 200) &&
          Array.isArray(res.data) &&
          res.data.length > 0
        ) {
          const firstActive =
            res.data.find((item) => item.status === "active") || res.data[0];
          bannerImage.value = firstActive.imageUrl || firstActive.image || "";
        }
      } catch (e) {
        bannerImage.value = "";
      }
    };

    onMounted(() => {
      loadMediaReports();
      loadBanner();
    });

    // 默认页面数据
    const getDefaultPageData = () => {
      return {
        title: "媒体报道",
        description: "媒体对展会的专业报道和深度分析",
      };
    };

    // 默认媒体报道数据
    const getDefaultMediaReports = () => {
      return [];
    };

    const setActiveTab = (tabKey) => {
      if (!tabKey) return;
      activeTab.value = tabKey;
      if (tabKey !== "media-reports") {
        window.location.href = `/${tabKey}`;
      }
    };
    const goDetail = (item) => {
      // console.log(item, "item");
      if (item.link) {
        window.open(item.link, "_blank");
        return;
      }
      router.push({
        name: "NewsDetails",
        query: {
          id: item.id,
          type: "media-reports",
          title: "媒体报道",
          banner: encrypt(bannerImage.value),
        },
      });
    };
    return {
      activeTab,
      bannerImage,
      pageData,
      mediaReports,
      setActiveTab,
      goDetail,
    };
  },
};
</script>

<style scoped>
.media-reports {
  min-height: 100vh;
  background: #f8f9fa;
}

.page-header {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  padding: 120px 0 80px 0;
  margin-top: 0;
  text-align: center;
  color: white;
}

.page-header.has-bg {
  height: 400px;
  position: relative;
  background: none;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.page-header.has-bg::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
}

.page-header.has-bg .container {
  position: relative;
  z-index: 1;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin-bottom: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.content-section {
  background: white;
  border-radius: 20px;
  padding: 3rem;
  margin: 2rem 0;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 1.6rem;
  color: #333;
  margin-bottom: 10px;
  text-align: left !important;
  position: relative;
  padding-left: 15px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
}
.section-title::after {
  display: none !important;
}

.section-title::before {
  content: "";
  position: absolute;
  left: 0;
  top: calc(50% - 10px);
  transform: translateY(-50%);
  width: 4px;
  height: 24px;
  background: linear-gradient(
    135deg,
    var(--e-global-color-a3699a4) 0%,
    var(--e-global-color-5c1ca3d) 100%
  );
  border-radius: 2px;
}
.section-desc {
  margin: -8px 0 2rem;
  color: #666;
  text-align: center;
  font-size: 1.1rem;
  line-height: 1.6;
}

/* 多条数据网格布局 */
.reports-grid {
  display: grid;
  grid-template-columns: repeat(1, 1fr);
  /* gap: 2rem; */
}

.report-item {
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  cursor: pointer;
}

/* 文本在下 */
.report-content {
  padding: 1rem 0.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #eee;
}

.report-t-b {
  gap: 5px;
  display: flex;
  align-items: center;
}

.report-content:hover .report-title {
  color: var(--e-global-color-selected);
}

.report-title {
  font-size: 1rem;
  font-weight: 600;
  color: #666666;
}

.report-source {
  color: #667eea;
  font-size: 0.9rem;
  font-weight: 500;
  margin-bottom: 1rem;
  text-align: center;
}

.report-content-text {
  color: #666;
  line-height: 1.6;
  text-align: left;
  margin-bottom: 1rem;
}

/* 富文本内图片限制 */
.report-content-text :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 10px 0;
  border-radius: 8px;
}

.report-excerpt {
  color: #666;
  line-height: 1.6;
  text-align: left;
  margin-bottom: 1rem;
}

.report-actions {
  text-align: center;
  padding-top: 1rem;
  border-top: 1px solid #eee;
}

.report-link {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.report-link:hover {
  color: var(--e-global-color-selected);
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }

  .content-section {
    padding: 2rem;
    margin: 1rem 0;
  }

  .section-title {
    font-size: 1.5rem;
  }

  .reports-grid {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }
}

/* 导航菜单样式 - 参考关于我们页面 */
.nav-menu {
  background: #f8f9fa;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.about-nav {
  display: flex;
  justify-content: center;
  gap: 30px;
  flex-wrap: wrap;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-size: 1.1rem;
  font-weight: 500;
  padding: 10px 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease, color 0.3s ease;
  border: 1px solid #eee;
  cursor: pointer;
}

.nav-item:hover {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

.nav-item.active {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

@media (max-width: 768px) {
  .about-nav {
    gap: 15px;
  }

  .nav-item {
    font-size: 1rem;
    padding: 8px 16px;
  }
}
</style>
